{extend name="template/base" /}

{block name="content"}
{block name="style"}
<style>
    .sct {
       width:20%;
	   float:left;
	   margin: 10px 10px 10px
    }
</style>
{/block}
<div class="page-container">
    <input type="hidden" id="callbackId" value="{$Request.param.id}">
    <div id="tab_upload" class="HuiTab">
        <div style='width:100%;height:20px'>
            &nbsp;
        </div>
        <div class="tabCon">
            <div>
                <span><b class="c-red">*</b>请选择一个知识点再上传</span>
                <!--<select id="select">-->
                    <!--<option value="" >请选择知识点</option>-->
                    <!--{volist name="knows" id="vo"}-->
                        <!--<option value="{$vo.id}" {$dataPoint==$vo.id ? 'selected' : ''}>{$vo.k_name}</option>-->
                    <!--{/volist}-->
                <!--</select>-->


                <div class="row cl">
                    <div class="formControls  col-xs-8 col-sm-5" style='width:98%' >
                        <div class="select-combox sbox" >
                          
							   <select  data-url="/admin/common_ajax/ajaxGetSections/subjectid/0/" autoload="/admin/common_ajax/ajaxGetSubject/" id="k_subjectid" name="k_subjectid" class="select-default combox autoloaditem sct" datatype="*" nullmsg="请选择所属科目" ref="k_sectionid" refUrl="/admin/common_ajax/ajaxGetSections/subjectid/{value}/" callback="getSectionContent">
									<option value>请选择科目</option>
									{volist name='subject' id='st'}
									<option value="{$st.id}" {$vo.k_subjectid==$st.id ? 'selected' : ''}>{$st.s_name}</option>
									{/volist}
								</select>
						
						
								<select name="k_sectionid" id="k_sectionid" class="select-default combox sct" datatype="*" nullmsg="请选择所属章节"  ref="k_knows" refUrl="/admin/common_ajax/ajaxGetknows/subjectid/0/sectionid/{value}/" callback="getSectionContent">
									<option value>请选择章节</option>
									{volist name='sections' id='st'}
									<option value="{$st.id}" {$vo.k_sectionid==$st.id ? 'selected' : ''}>{$st.se_name}</option>
									{/volist}
								</select>
							
                            <select name="k_sectionid" id="k_knows" class="select-default sct" datatype="*" nullmsg="请选择所属知识点">
                                <option value>请选择知识点</option>

                            </select>
							<div class=" sct">
								<a class="btn btn-primary radius mr-5" href="__STATIC__/files/shiti.xls" target='_blank' > 标准模板下载
								</a>
							</div>
                        </div>
                    </div>
                    <div class="col-xs-offset-4"></div>
                </div>

                <div class="row cl">
                    <div class="formControls col-xs-8 col-sm-5">
                        当前选择的知识点为：<span class="c-red" style="color:#eb6100" >{if isset($dataPoint)}{$knows[$dataPoint]['k_name']}{else}未选择知识点{/if}</span>
                    </div>
                    <div class="col-xs-offset-4"></div>
                </div>

                {volist name="Type" id="tp"}
                    <div>{$tp.type}请填写:{$tp.id}</div>
                {/volist}
                <div style='width:100%;height:10px;'>
                    &nbsp;
                </div>
                <!--<input id="fileupload" id="fileupload" type="file" name="file" data-url="{:\\think\\Url::build('uploadexcel',['qbankid'=>$qbankid,'datapoint'=> $dataPoint])}">-->
				
				<div class="row cl">
				
					<div class="formControls col-xs-6 col-sm-6">
					<span class="c-red">*</span>EXCEL：	<input type="text" class="input-text" id="upload" name='file' placeholder="请点击后面的上传按钮" datatype="*" nullmsg="请填写图片url" style="width: 50%">

						<button type="button" class="btn btn-primary radius" onclick=' $("#fileupload").click();' >上传</button>
						<a href='javascript:;' type="button" class="btn btn-success radius ssubmit" data-url="{:\\think\\Url::build('uploadexcel2',['qbankid'=>$qbankid,'datapoint'=> $dataPoint,'sectionid'=> $sectionid,'subjectid'=> $subjectid])}" >确认</a>
						<input id="fileupload" id="fileupload" type="file" name="file" data-url="{:\\think\\Url::build('uploadexcel1',['qbankid'=>$qbankid,'datapoint'=> $dataPoint,'sectionid'=> $sectionid,'subjectid'=> $subjectid])}" style='display:none' >
					
				</div>
			
            </div>
        </div>
    </div>
</div>
{/block}
{block name="style"}
<link rel="stylesheet" href="__LIB__/lightbox2/css/lightbox.min.css">
{/block}
{block name="script"}
<script src="__LIB__/laypage/1.3/laypage.js"></script>
<!--<script src="__LIB__/tpUpload/tpUpload.js"></script>-->
<script src="__LIB__/jQuery-File-Upload/js/vendor/jquery.ui.widget.js"></script>
<script src="__LIB__/jQuery-File-Upload/js/jquery.iframe-transport.js"></script>
<script src="__LIB__/jQuery-File-Upload/js/jquery.fileupload.js"></script>
<script src="__LIB__/lightbox2/js/lightbox.min.js"></script>
<script>

    /*
     // uploadify 上传，需要可以自己使用
     $("#file-input").uploadify({
     'fileObjName' : 'file',
     'buttonText' : '选择文件',
     'buttonCursor' : 'pointer',
     'multi' : false,
     'fileSizeLimit' : '5MB',
     'fileTypeExts' : '*.gif; *.jpg; *.png',
     'swf'      : '__LIB__/uploadify/uploadify.swf',
     'uploader' : '{:\\think\\Url::build("upload")}',
     'onUploadError' : function (file, errorCode, errorMsg, errorString) {
     layer.alert('文件'+file.name+'上传失败：'+errorString);
     },
     'onUploadSuccess' : function (file, ret, response) {
     console.log(arguments);
     },
     'onFallback' : function () {
     layer.alert('您的浏览器不支持Flash文件上传');
     }
     });*/
    $(function () {
        var callbackId = $("#callbackId").val();
        // Tab 切换
        $.tpTab("#tab_upload .tabBar span", "#tab_upload .tabCon", "current", "click", "0", function (index, tabCon, tabBar) {
            if (index == 2 && tabCon.eq(index).find('.photo-list').html() == '') {
                getListImage({p: p, 'count': '1'});
            }
        }, function (i) {

        });
        //
        $("#k_knows").change(function () {
            var selectVal = $('#k_knows').val();
            var k_sectionid = $('#k_sectionid').val();
            var k_subjectid = $('#k_subjectid').val();
            changeDataPoint(selectVal,k_sectionid,k_subjectid);
        });
		
		 $(".ssubmit").click(function () {
			var datafile = $(".ssubmit").attr("data-url");
			var fileusrl = $("#upload").val();
			if(fileusrl==''){
				layer.msg('请选择要上传得文件', {time: 2000});
				return false;
			}

           $.post(datafile, {'url':fileusrl}, function (ret) {
				layer.msg(ret, {time: 2000});
		   });
			
		   
        });
		
	
        
        var dataUrlNew = $("#fileupload").attr("data-url");
       
        $('#fileupload').fileupload({
            url: dataUrlNew,
            dataType: 'json',
            dropZone: $('#drag'),
			 add: function (e, data) {
                var uploadErrors = [];
				
				 var acceptFileTypes = /\/(csv|xls|vnd.ms-excel)$/i;
				if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])){
					uploadErrors.push('只能上传ecxel文件');
				}
 

              
                if (uploadErrors.length > 0) {
                    alert(uploadErrors.join("\n"));
                } else {

                    data.submit();
                }

            },
            start: function () {
                layer_msg = layer.msg('正在上传中…', {time: 100000000});
            },
            progressall: function (e, data) {
                $('.layui-layer-msg .layui-layer-content').html('已上传' + (data.loaded / data.total * 100).toFixed(2) + '%');
            },
            done: function (e, data) {
			  document.getElementById('upload').value =data.result.data.name;
                layer.close(layer_msg);
                callback(callbackId, data.result.data.name);
				//parent.layer.closeAll();
            }
        });
        $('#drag').bind('drop dragover', function (e) {
            e.preventDefault();
        }).on('dragenter', function (e) {
            $(this).addClass('dragenter');
        }).on('drop', function (e) {
            $(this).removeClass('dragenter');
        }).on('dragleave', function (e) {
            $(this).removeClass('dragenter');
        });
        ;
        // 文件上传
        /*$("#file-input").tpUpload({
         url: '{:\\think\\Url::build("upload")}',
         data: {a: 'a'},
         drag: '#drag',
         start: function () {
         layer_msg = layer.msg('正在上传中…', {time: 100000000});
         },
         progress: function (loaded, total, file) {
         $('.layui-layer-msg .layui-layer-content').html('已上传' + (loaded / total * 100).toFixed(2) + '%');
         },
         success: function (ret) {
         callback(callbackId,ret.data[0]);
         },
         error: function (ret) {
         layer.alert(ret);
         },
         end: function () {
         layer.close(layer_msg);
         }
         });*/

        // 远程图片抓取
        $("#net-image").on('click', '#crawl-btn', function () {
            var remote_input = $("#remote-input");
            if (!remote_input.val()) {
                layer.alert('请输入远程图片的链接');
                return;
            }
            ajax_req('{:\\think\\Url::build("remote")}', {'url': remote_input.val()}, function (ret) {
                remote_input.val(ret.data.url);
            }, undefined, true);
        }).on('click', '#remote-btn', function () {
            var url = $("#remote-input").val();
            callback(callbackId, url);
        });

        // 已上传图片列表
        // 初始化
        var p = location.hash.replace('#!p=', '') || 1;
        var pages = 0;
        $(".photo-list").on("click", '.photo-list-select', function () {
            callback(callbackId, $(this).parent().prev().attr('data-src'))
        });

        function getListImage(param) {
            $.post('{:\\think\\Url::build("listImage")}', param, function (ret) {
                if (ret.code) {
                    layer.alert(ret.msg);
                } else {
                    if (typeof ret.data.count != "undefined" && ret.data.count > 0) {
                        pages = Math.ceil(ret.data.count / 10);
                    }

                    // 数据组装
                    var html = '';
                    for (var i in ret.data.list) {
                        var current = ret.data.list[i];
                        html += '<div class="photo-list-item">'
                                + '<img src="' + current.name + '" data-src="' + current.name + '" class="photo-sub" alt="' + current.original + '" title="' + current.original + '">'
                                + '<div class="photo-mask">'
                                + '<a class="photo-list-btn photo-list-preview radius" href="' + current.name + '" data-lightbox="preview" data-title="' + current.original + '">预览</a>'
                                + '<a class="photo-list-btn photo-list-select radius ml-10" href="javascript:;">选择</a>'
                                + '</div>'
                                + '</div>';
                    }
                    $(".photo-list").html(html);
                    //显示分页
                    laypage({
                        cont: 'photo-page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                        pages: pages, //通过后台拿到的总页数
                        curr: param.p, //当前页
                        hash: 'p',
                        jump: function (obj, first) { //触发分页后的回调
                            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                getListImage({'p': obj.curr});
                            }
                        }
                    });
                }
            });
        }
    });

    /**
     * 数据回调
     * @param id
     * @param value
     */
    function callback(id, value) {
        if (window.parent.frames.length == 0) {
            layer.alert('请在弹层中打开此页');
        } else {
//            parent.document.getElementById(id).value = value;
            layer.alert('上传成功');
			
            //$.delay(3000);
//            
        }
    }

    function changeDataPoint(point,k_sectionid,k_subjectid) {
        $.ajax({
            //提交数据的类型 POST GET
            type: "POST",
            //提交的网址
            url: "{:\\think\\Url::build('index/changePoint')}",
            //提交的数据
            data: {Point: point,sectionid:k_sectionid, subjectid:k_subjectid},
            //返回数据的格式
            datatype: "json",
            success: function (data) {
                window.location.reload();
            },
        });
    }
	
	
	
	
</script>
{/block}